local assets1 = {
	Asset("ANIM", "anim/musha_yamche_egg.zip"),
	Asset("ATLAS", "images/inventoryimages/musha_yamche_egg.xml"),
	Asset("IMAGE", "images/inventoryimages/musha_yamche_egg.tex"),
	Asset("IMAGE", "images/inventoryimages/musha_yamche_egg_cracked.tex"),
	Asset("ATLAS", "images/inventoryimages/musha_yamche_egg_cracked.xml")
}

local assets2 = {
	Asset("ANIM", "anim/musha_yamche_egg2.zip"),
	Asset("ATLAS", "images/inventoryimages/musha_yamche_egg2.xml"),
	Asset("IMAGE", "images/inventoryimages/musha_yamche_egg2.tex"),
	Asset("IMAGE", "images/inventoryimages/musha_yamche_egg2_cracked.tex"),
	Asset("ATLAS", "images/inventoryimages/musha_yamche_egg2_cracked.xml")
}

local assets3 = {
	Asset("ANIM", "anim/musha_yamche_egg3.zip"),
	Asset("ATLAS", "images/inventoryimages/musha_yamche_egg3.xml"),
	Asset("IMAGE", "images/inventoryimages/musha_yamche_egg3.tex"),
	Asset("IMAGE", "images/inventoryimages/musha_yamche_egg3_cracked.tex"),
	Asset("ATLAS", "images/inventoryimages/musha_yamche_egg3_cracked.xml")
}

local assets4 = {
	Asset("ANIM", "anim/musha_yamche_egg4.zip"),
	Asset("ATLAS", "images/inventoryimages/musha_yamche_egg4.xml"),
	Asset("IMAGE", "images/inventoryimages/musha_yamche_egg4.tex"),
	Asset("IMAGE", "images/inventoryimages/musha_yamche_egg4_cracked.tex"),
	Asset("ATLAS", "images/inventoryimages/musha_yamche_egg4_cracked.xml")
}

local assets_musha_beefalo_egg = {
	Asset("ANIM", "anim/musha_beefalo_egg.zip"),
	Asset("ATLAS", "images/inventoryimages/musha_beefalo_egg.xml"),
	Asset("IMAGE", "images/inventoryimages/musha_beefalo_egg.tex")
}

local assets_musha_beefalo_egg_cracked = {
	Asset("ANIM", "anim/musha_beefalo_egg.zip"),
	Asset("IMAGE", "images/inventoryimages/musha_beefalo_egg_cracked.tex"),
	Asset("ATLAS", "images/inventoryimages/musha_beefalo_egg_cracked.xml")
}

local egg_to_animal = {
	{["musha_yamche_egg"] = {name = "musha_yamche", level = 0}},
	{["musha_yamche_egg2"] = {name = "musha_yamche", level = 80}},
	{["musha_yamche_egg3"] = {name = "musha_yamche", level = 900}},
	{["musha_yamche_egg4"] = {name = "musha_yamche", level = 1800}},
	{["musha_yamche_egg_cracked"] = {name = "musha_yamche", level = 0}},
	{["musha_yamche_egg2_cracked"] = {name = "musha_yamche", level = 450}},
	{["musha_yamche_egg3_cracked"] = {name = "musha_yamche", level = 900}},
	{["musha_yamche_egg4_cracked"] = {name = "musha_yamche", level = 1800}},
	{["musha_beefalo_egg"] = {name = "musha_babybeefalo"}},
	{["musha_beefalo_egg_cracked"] = {name = "musha_babybeefalo"}}
}

local function CheckHatch(inst)
	if inst.components.hatchable.state == "hatch" then
		local animal_name = egg_to_animal[inst.prefab].name
		local animal = SpawnPrefab(animal_name)
		if animal_name == "musha_yamche" then
			animal.data.currentlevel = egg_to_animal[inst.prefab].level
		end
		local owner = UserToPlayer(inst.ownerid)
		if owner and owner.components.musha_leader and not owner.components.musha_leader.musha_followers[animal_name] then
			owner.components.musha_leader:AddFollower(animal_name, animal)
		end
		animal.Transform:SetPosition(inst.Transform:GetWorldPosition())
		animal.sg:GoToState("hatch")
		inst:Remove()
	end
end

local function PlayUncomfySound(inst)
	inst.SoundEmitter:KillSound("uncomfy")
	if inst.components.hatchable.toohot then
		inst.SoundEmitter:PlaySound("dontstarve/creatures/egg/egg_hot_steam_LP", "uncomfy")
	elseif inst.components.hatchable.toocold then
		inst.SoundEmitter:PlaySound("dontstarve/creatures/egg/egg_cold_shiver_LP", "uncomfy")
	end
end

local function OnDropped(inst)
	musha_print("OnDropped")
	inst.components.hatchable:StartUpdating()
	CheckHatch(inst)
	PlayUncomfySound(inst)
	inst.Light:Enable(true)
end

local function OnPutInInventory(inst, owner)
	inst.ownerid = owner.userid
	inst.components.hatchable:StopUpdating()
	inst.SoundEmitter:KillSound("uncomfy")
	inst.Light:Enable(false)
end

local function DropLoot(inst)
	inst:AddComponent("lootdropper")
	if inst.components.hatchable.toohot then
		inst.components.lootdropper:SetLoot({})
	else
		inst.components.lootdropper:SetLoot({})
	end
	inst.components.lootdropper:DropLoot()
end

local function OnHatchState(inst, state)
	musha_print("OnHatchState", state)
	inst.SoundEmitter:KillSound("uncomfy")
	if state == "crack" then
		local cracked = SpawnPrefab(inst.prefab .. "_cracked")
		cracked.Transform:SetPosition(inst.Transform:GetWorldPosition())
		cracked.AnimState:PlayAnimation("crack")
		cracked.AnimState:PushAnimation("idle_happy", true)
		cracked.SoundEmitter:PlaySound("dontstarve/creatures/egg/egg_hatch_crack")
		inst:Remove()
	elseif state == "uncomfy" then
		if inst.components.hatchable.toohot then
			inst.AnimState:PlayAnimation("idle_hot", true)
		elseif inst.components.hatchable.toocold then
			inst.AnimState:PlayAnimation("idle_cold", true)
		end
		PlayUncomfySound(inst)
	elseif state == "comfy" then
		inst.AnimState:PlayAnimation("idle_happy", true)
	elseif state == "hatch" then
		CheckHatch(inst)
	elseif state == "dead" then
		if inst.components.hatchable.toohot then
			inst.SoundEmitter:PlaySound("dontstarve/creatures/egg/egg_hot_jump")
			inst:DoTaskInTime(20 * FRAMES, PlaySound, "dontstarve/creatures/egg/egg_hot_explo")
			inst:DoTaskInTime(20 * FRAMES, DropLoot)
			inst.AnimState:PlayAnimation("toohot")
		elseif inst.components.hatchable.toocold then
			inst:DoTaskInTime(15 * FRAMES, PlaySound, "dontstarve/creatures/egg/egg_cold_freeze")
			inst:DoTaskInTime(30 * FRAMES, DropLoot)
			inst.AnimState:PlayAnimation("toocold")
		end

		inst.components.inventoryitem.canbepickedup = false
		inst.persists = false
		inst:ListenForEvent("animover", inst.Remove)
	end
end

local function OnNear(inst)
	if
		inst.components.playerprox ~= nil and inst.components.playerprox:IsPlayerClose() and
			inst.components.hatchable.state == "hatch" and
			not inst.components.inventoryitem:IsHeld()
	 then
		CheckHatch(inst)
	end
end

local function common_fn(name)
	local inst = CreateEntity()

	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	--inst.entity:AddDynamicShadow()
	inst.entity:AddNetwork()

	MakeInventoryPhysics(inst)

	inst.AnimState:SetBank("egg")
	inst.AnimState:SetBuild(name)
	inst.AnimState:PlayAnimation("egg")

	inst.entity:AddLight()
	inst.Light:SetRadius(1)
	inst.Light:SetFalloff(.7)
	inst.Light:SetIntensity(.5)
	inst.Light:SetColour(150 / 255, 150 / 255, 150 / 255)
	inst.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
	inst.Light:Enable(true)

	--inst.entity:AddMiniMapEntity()
	--inst.MiniMapEntity:SetIcon(name .. ".tex")
	--inst.MiniMapEntity:SetDrawOverFogOfWar(true)

	inst.entity:SetPristine()

	if not TheWorld.ismastersim then
		return inst
	end

	inst:AddComponent("inspectable")

	inst:AddComponent("inventoryitem")
	inst.components.inventoryitem:SetOnDroppedFn(OnDropped)
	inst.components.inventoryitem:SetOnPutInInventoryFn(OnPutInInventory)
	inst.components.inventoryitem.atlasname = "images/inventoryimages/" .. name .. ".xml"

	inst:AddComponent("cookable")
	inst.components.cookable.product = "tallbirdegg_cooked"

	inst:AddComponent("hatchable")
	inst.components.hatchable:SetOnState(OnHatchState)
	inst.components.hatchable:SetCrackTime(TUNING.TOTAL_DAY_TIME * 0.5)
	inst.components.hatchable:SetHatchTime(TUNING.TOTAL_DAY_TIME)
	inst.components.hatchable:SetHatchFailTime(TUNING.TOTAL_DAY_TIME * 0.25)
	inst.components.hatchable:SetHeaterPrefs(false, nil, true)
	inst.components.hatchable:StartUpdating()

	inst.components.inspectable.getstatus = inst.components.hatchable.GetDebugString

	inst.OnLoadPostPass = function(inst)
		if inst.components.inventoryitem:IsHeld() then
			inst.components.hatchable:StopUpdating()
			inst.SoundEmitter:KillSound("uncomfy")
			inst.Light:Enable(false)
		end
	end

	inst.OnSave = function(inst, data)
		data.ownerid = inst.ownerid
	end

	inst.OnLoad = function(inst, data)
		inst.ownerid = data.ownerid
	end

	return inst
end

local function cracked_common_fn(name)
	local inst = common_fn(name)

	if not TheWorld.ismastersim then
		return inst
	end

	inst.components.hatchable.state = "comfy"
	inst.components.hatchable:StartUpdating()
	inst.components.inventoryitem.atlasname = "images/inventoryimages/" .. name .. "_cracked.xml"

	inst:AddComponent("playerprox")
	inst.components.playerprox:SetDist(6, 6)
	inst.components.playerprox:SetOnPlayerNear(OnNear)

	return inst
end

local function musha_yamche_egg_fn()
	return common_fn("musha_yamche_egg")
end

local function musha_yamche_egg2_fn()
	return common_fn("musha_yamche_egg2")
end

local function musha_yamche_egg3_fn()
	return common_fn("musha_yamche_egg3")
end

local function musha_yamche_egg4_fn()
	return common_fn("musha_yamche_egg4")
end

local function musha_yamche_egg_cracked_fn()
	return cracked_common_fn("musha_yamche_egg_cracked")
end

local function musha_yamche_egg2_cracked_fn()
	return cracked_common_fn("musha_yamche_egg2_cracked")
end

local function musha_yamche_egg3_cracked_fn()
	return cracked_common_fn("musha_yamche_egg3_cracked")
end

local function musha_yamche_egg4_cracked_fn()
	return cracked_common_fn("musha_yamche_egg4_cracked")
end

local function musha_beefalo_egg()
	local inst = common_fn("musha_beefalo_egg")
	inst.components.hatchable:SetCrackTime(TUNING.TOTAL_DAY_TIME * 0.3)
	inst.components.hatchable:SetHatchFailTime(TUNING.TOTAL_DAY_TIME * 0.1)
	return inst
end
local function musha_beefalo_egg_cracked()
	local inst = cracked_common_fn("musha_beefalo_egg_cracked")
	inst.components.hatchable:SetHatchTime(TUNING.TOTAL_DAY_TIME * 0.7)
	inst.components.hatchable:SetHatchFailTime(TUNING.SMALLBIRD_HATCH_TIME * 0.3)
	return inst
end

return Prefab("musha_yamche_egg", musha_yamche_egg_fn, assets1), Prefab(
	"musha_yamche_egg_cracked",
	musha_yamche_egg_cracked_fn,
	assets1
), Prefab("musha_yamche_egg2", musha_yamche_egg2_fn, assets2), Prefab(
	"musha_yamche_egg2_cracked",
	musha_yamche_egg2_cracked_fn,
	assets2
), Prefab("musha_yamche_egg3", musha_yamche_egg3_fn, assets3), Prefab(
	"musha_yamche_egg3_cracked",
	musha_yamche_egg3_cracked_fn,
	assets3
), Prefab("musha_yamche_egg4", musha_yamche_egg4_fn, assets4), Prefab(
	"musha_yamche_egg4_cracked",
	musha_yamche_egg4_cracked_fn,
	assets4
), Prefab("musha_beefalo_egg", musha_beefalo_egg, assets_musha_beefalo_egg), Prefab(
	"musha_beefalo_egg_cracked",
	musha_beefalo_egg_cracked,
	assets_musha_beefalo_egg_cracked
)
